
//
//  FunnyTableViewCell.m
//  HaHa
//
//  Created by lanou3g on 15/9/18.
//  Copyright (c) 2015年 吉亚平. All rights reserved.
//

#import "FunnyTableViewCell.h"

@implementation FunnyTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self AddUpCell];
        
    }
    return self;
}

- (void)AddUpCell
{
    CGFloat width = kScreenWdth - 20;
    //发表者头像
    _HeadPortraitPic = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];
    _HeadPortraitPic.layer.cornerRadius = _HeadPortraitPic.width / 2;
    _HeadPortraitPic.layer.masksToBounds = YES;
    [self.contentView addSubview:_HeadPortraitPic];

    //分享
    _shareButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    _shareButton.frame = CGRectMake(width - 30 , _HeadPortraitPic.Y , 30, 40);
    [_shareButton addTarget:self action:@selector(share:) forControlEvents:(UIControlEventTouchUpInside)];
    [_shareButton setTitle:@"分享" forState:(UIControlStateNormal)];
    [self.contentView addSubview:_shareButton];

    //昵称
    _userName = [[UILabel alloc] initWithFrame:CGRectMake(_HeadPortraitPic.X_width + 5, _HeadPortraitPic.Y, width - _HeadPortraitPic.X_width - _shareButton.width - 10, _HeadPortraitPic.height)];

    [self.contentView addSubview:_userName];

    //人气标题
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _userName.Y_height + 5,  width, 0)];

    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.numberOfLines = 0;
    
    [self.contentView addSubview:_titleLabel];
    
    //图片
    _imagePic = [[UIImageView alloc] initWithFrame:CGRectMake(0, _titleLabel.Y_height + 5, kScreenWdth-20, 20)];
    [self.contentView addSubview:_imagePic];
    _commentBackView = [[UIView alloc] init];
    //评论者头像
    _commentsPic = [[UIImageView alloc] initWithFrame:CGRectMake(5, _imagePic.Y_height + 5, 30, 30)];
    _commentsPic.layer.cornerRadius = _commentsPic.width / 2;
    //允许编辑
    _commentsPic.layer.masksToBounds = YES;
    [_commentBackView addSubview:_commentsPic];

    //评论者名字
    _commentsName = [[UILabel alloc] initWithFrame:CGRectMake(_commentsPic.X_width + 5, _commentsPic.Y, width - _commentsPic.X_width, _commentsPic.height)];
    _commentsName.textColor = KgrayColor;
    [_commentBackView addSubview:_commentsName];


    //评论
    _commentsTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, _commentsName.Y_height + 5, kScreenWdth - 20, 0)];
        [_commentBackView addSubview:_commentsTitle];

        [self.contentView addSubview:_commentBackView];
    
    
    _buttonBackView = [[UIView alloc] initWithFrame:CGRectMake(15, _imagePic.Y_height + 5, kScreenWdth-40, 30)];
     //   _buttonBackView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    
    UIImageView *praise = [[UIImageView alloc] initWithFrame:CGRectMake(40,0, 30, 30)];
    praise.image = [UIImage imageNamed:@"praise"];
    [self.buttonBackView addSubview:praise];
    _praiseCount = [[UILabel alloc] initWithFrame:CGRectMake(65, 0,40 , 30)];
    _praiseCount.textColor = KgrayColor;
    _praiseCount.font = [UIFont systemFontOfSize:11];
    [_buttonBackView addSubview:_praiseCount];
    
    UIImageView *buried = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWdth/2-30, 0, 30, 30)];
    buried.image = [UIImage imageNamed:@"buried"];
    [_buttonBackView addSubview:buried];
    
    _buried = [[UILabel alloc] initWithFrame:CGRectMake(buried.X_width-5, 0, 40, 30)];
    _buried.font = [UIFont systemFontOfSize:11];
    _buried.textColor = KgrayColor;
    [self.buttonBackView addSubview:_buried];
    
    UIImageView *Comment = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWdth-100, 0, 30, 30)];
    Comment.image = [UIImage imageNamed:@"Comment"];
    [self.buttonBackView addSubview:Comment];
    
    self.commentCount = [[UILabel alloc] initWithFrame:CGRectMake(Comment.X_width-5, 0, 40, 30)];
    _commentCount.font = [UIFont systemFontOfSize:11];
    _commentCount.textColor = KgrayColor;
    [self.buttonBackView addSubview:self.commentCount];
    
    
    
    
    
    
    [self.contentView addSubview:_buttonBackView];

    
    
    
    
}



- (void)share:(UIButton *)button
{
    NSLog(@"分享");
}



- (void)setValueWithModel:(FunnyModel *)model
{
    //取出url_list中的网址
    NSArray *urlArr = [model.large_image objectForKey:@"url_list"];
    //将最后一个网址取出来给url
    NSDictionary *url = [urlArr firstObject];
    //用字符串接收
    NSString *URL = [url objectForKey:@"url"];
    //取出avatar_url  用字符串接收
    NSString *headerPic = [model.user objectForKey:@"avatar_url"];
    
    NSDictionary *commentsArr = [model.comments firstObject];
    //接收图片
    NSString *comentsPic = [commentsArr objectForKey:@"user_profile_image_url"];
    
    NSString *commentName = [commentsArr objectForKey:@"user_name"];
    
    NSString *commentText = [commentsArr objectForKey:@"text"];
    //文字高度
    CGFloat textheight = [self YYHreturnStringHeight:model.text Width:kScreenWdth-20 Font:17];
    _titleLabel.frame = CGRectMake(0, _userName.Y_height + 5,  kScreenWdth-20, textheight);
    //图片高度
    NSString *widthTemp = [model.large_image objectForKey:@"width"];
    NSString *imagehieghtTemp = [model.large_image objectForKey:@"height"];
    NSInteger width = [widthTemp integerValue];
    NSInteger height = [imagehieghtTemp integerValue];
    
    //如果大于表视图的宽度 等比缩放到与表视图等宽
    //如果小于表视图宽度 等于表视图宽度
    CGFloat imageWidth;
    CGFloat imageHeight;
    if (width>kScreenWdth-20) {
        CGFloat beishu = width/(kScreenWdth-20);
        imageHeight = height/beishu;
        imageWidth = kScreenWdth-20;
    }else if(height == 0){
        imageHeight = kScreenWdth-20;
        imageWidth = kScreenWdth-20;
    }else{
        imageHeight = height;
        imageWidth = width;
    }
    _imagePic.frame = CGRectMake((kScreenWdth-20) / 2-imageWidth / 2, _titleLabel.Y_height+5, imageWidth, imageHeight);
    [self.imagePic sd_setImageWithURL:[NSURL URLWithString:URL] placeholderImage:[UIImage imageNamed:nil]];
    [self.HeadPortraitPic sd_setImageWithURL:[NSURL URLWithString:headerPic]  placeholderImage:nil];
    if (model.comments.count == 0) {
        self.commentBackView.hidden = YES;
    }else{
        self.commentBackView.hidden = NO;
    }
    //给用户名赋值
    self.userName.text = [model.user objectForKey:@"name"];
    //给图片标题赋值
    self.titleLabel.text = model.text;
    //给评论者头像赋值
    [self.commentsPic sd_setImageWithURL:[NSURL URLWithString:comentsPic] placeholderImage:nil];
     //给评论人赋值
    self.commentsName.text = commentName ;
    //给评论赋值
    self.commentsTitle.text = commentText;
    
    self.praiseCount.text = [NSString stringWithFormat:@"%ld",model.favorite_count];
    self.buried.text = [NSString stringWithFormat:@"%ld",model.bury_count];
    self.commentCount.text = [NSString stringWithFormat:@"%ld",model.comment_count];
    
    [self SetFrameSS];
    //评论内容高度
    NSDictionary *tempDic = [model.comments firstObject];
    NSString *CommentText = [tempDic objectForKey:@"text"];
    CGFloat CommentTextheight = [self YYHreturnStringHeight:CommentText Width:kScreenWdth-20 Font:17];
    
    if (model.comments.count != 0) {
        _commentsTitle.frame = CGRectMake(0, _commentsName.Y_height, kScreenWdth - 20, CommentTextheight + 30);
        _commentsTitle.numberOfLines = 0; 
    }
     _commentBackView.frame = CGRectMake(0, _imagePic.Y_height+5, kScreenWdth-20, _commentsPic.Y_height+_commentsTitle.height + 5);
    if (_commentsTitle.text == 0) {
        _buttonBackView.frame = CGRectMake(15, _imagePic.Y_height + 5, kScreenWdth-40, 30);
    }else{
        _buttonBackView.frame = CGRectMake(15, _commentsTitle.Y_height + 5, kScreenWdth-40, 30);
    }
    
}

- (void)SetFrameSS{
    
    UIImageView *comments = [[UIImageView alloc] initWithFrame:CGRectMake(1, _commenBackView.Y_height +10, 30, 12)];
    comments.image = [UIImage imageNamed:@"神评论.png"];
    [self.commentBackView addSubview:comments];
    //评论者头像
    _commentsPic.frame = CGRectMake(5, 18, 30, 30);
    //评论者名字
    _commentsName.frame = CGRectMake(_commentsPic.X_width + 5, _commentsPic.Y, kScreenWdth-20 - _commentsPic.X_width, _commentsPic.height);
    //评论
    
    
    
    
    

    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

//计算高度
- (CGFloat)YYHreturnStringHeight:(NSString *)string Width:(CGFloat)Width Font:(int)font{
    
    CGRect frame = [string boundingRectWithSize:CGSizeMake(Width, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName] context:nil];
    return frame.size.height;
}
@end
